package com.zyy.study.always.code4;

public class BankSimulation {
    public static void main(String[] args) throws InterruptedException {
        Bank bank = new Bank();

        // 创建三个账户
        bank.createAccount("A001", "张三", 1000);
        bank.createAccount("A002", "李四", 2000);
        bank.createAccount("A003", "王五", 3000);

        // 获取账户引用
        BankAccount account1 = bank.getAccount("A001");
        BankAccount account2 = bank.getAccount("A002");
        BankAccount account3 = bank.getAccount("A003");

        // 创建存款线程
        Thread deposit1 = new Thread(() -> account1.deposit(100), "存款线程1");
        Thread deposit2 = new Thread(() -> account2.deposit(200), "存款线程2");
        Thread deposit3 = new Thread(() -> account3.deposit(300), "存款线程3");

        // 创建取款线程
        Thread withdraw1 = new Thread(() -> account1.withdraw(50), "取款线程1");
        Thread withdraw2 = new Thread(() -> account2.withdraw(150), "取款线程2");
        Thread withdraw3 = new Thread(() -> account3.withdraw(250), "取款线程3");

        // 启动所有线程
        deposit1.start();
        deposit2.start();
        deposit3.start();
        withdraw1.start();
        withdraw2.start();
        withdraw3.start();

        // 等待所有线程完成
        deposit1.join();
        deposit2.join();
        deposit3.join();
        withdraw1.join();
        withdraw2.join();
        withdraw3.join();

        // 打印最终余额
        bank.printAllBalances();
    }
}
